﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using BawangMemo.BackendModels;

namespace BawangMemo.BawangMemoView.View
{
    /// <summary>
    /// ListShowView.xaml 的交互逻辑
    /// </summary>
    public partial class ListShowView : UserControl
    {
        private List<WordWrapper> _wordList;
        private WordWrapper _word;
        private int listId;
        private CourseWrapper course;

        public ListShowView(CourseWrapper cw, int listId)
        {
            InitializeComponent();

            course = cw;
            InitList(listId);

            //button_review.IsEnabled = false;
            //button_test.IsEnabled = false;
        }

        private void InitList(int listId)
        {
            label2.Content = "List " + listId;
            this.listId = listId;
            _wordList = ListRepository.GetList(course, listId);

            UpdateWordListBox();
            if (listBox1.Items.Count > 0)
                listBox1.SelectedIndex = 0;
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
                _word = (WordWrapper)listBox1.SelectedItem;
            UpdateWordView();
            UpdateProgressBar(progressBar1);

            this.button_back.IsEnabled = true;
            this.button_go.IsEnabled = true;
            if (listBox1.SelectedIndex == 0)
            {
                this.button_back.IsEnabled = false;
            }
            if (listBox1.SelectedIndex + 1 >= listBox1.Items.Count)
            {
                this.button_go.IsEnabled = false;
            }
        }

        private void UpdateWordListBox()
        {
            listBox1.ItemsSource = _wordList;
            listBox1.SelectedIndex = -1;
        }

        private void UpdateWordView()
        {
            if (_word != null)
            {
                wordView1.SetWord(_word);
                wordView1.ToggleExplanations(wordView1.Contents.Keys.ToArray<string>(), true);
            }
        }

        private void wordView1_TagEventHandler(object sender, RoutedEventArgs e)
        {
            TagEventArgs args = (TagEventArgs)e;
            //DEBUG: wintokk
            WordWrapper word = wordView1.GetWord();
            //MessageBox.Show(word.Id + " " + word.Spell + " " + args.Tag);
            if (args.IsNew)
                word.AddTag(args.Tag);
            else
                word.RemoveTag(args.Tag);

            wordView1.Tags = TagRepository.ListWordTags(word);
        }

        private void button_go_Click(object sender, RoutedEventArgs e)
        {
            if (listBox1.SelectedIndex + 1 >= listBox1.Items.Count)
            {
                MessageBox.Show("这是最后一个单词啦:-)");
            }
            else
            {
                listBox1.SelectedIndex++;
            }
        }

        private void UpdateProgressBar(ProgressBar progressBar1)
        {
            progressBar1.Value = (listBox1.SelectedIndex + 1.0) / listBox1.Items.Count * 100;
        }

        private void button_back_Click(object sender, RoutedEventArgs e)
        {
            if (listBox1.SelectedIndex == 0)
            {
                MessageBox.Show("这是第一个单词:-)");
            }
            else
            {
                listBox1.SelectedIndex--;
            }
        }

        private void button_prevList_Click(object sender, RoutedEventArgs e)
        {
            int count = ListRepository.GetListCount(course);
            if (listId <= 1)
            {
                MessageBox.Show("这是第一个List :-)");
            }
            else
            {
                --listId;
                //InitList(listId);
                MainWindowManager._mainWindow.grid1.Children.Clear();
                MainWindowManager._mainWindow.grid1.Children.Add(new ListShowView(course, listId));
            }
        }

        private void button_nextList_Click(object sender, RoutedEventArgs e)
        {
            int count = ListRepository.GetListCount(course);
            if (listId >= count)
            {
                MessageBox.Show("这是最后一个List :-)");
            }
            else
            {
                ++listId;
//                InitList(listId);
                            MainWindowManager._mainWindow.grid1.Children.Clear();
                MainWindowManager._mainWindow.grid1.Children.Add(new ListShowView(course, listId));
            }
        }

        public static int ReviewSize = 10;

        private void button_review_Click(object sender, RoutedEventArgs e)
        {
            int reviewCount;
            if (Int32.TryParse(textBox1.Text, out reviewCount))
            {
                ReviewSize = 10;
            }
            else
            {
                MessageBox.Show("非法数字");
                textBox1.Focus();
                textBox1.SelectAll();
                return;
            }

            int current = listBox1.SelectedIndex;
            int start = current - ReviewSize + 1;
            if (start < 0)
                start = 0;

            List<WordWrapper> reviewList = new List<WordWrapper>();
            int k = listBox1.SelectedIndex;
            for (int i = start; i <= current; i++)
            {
                reviewList.Add(_wordList[i]);
            }

            ReviewWindow window = new ReviewWindow();
            ReviewView view = new ReviewView(reviewList, window);
            window.grid1.Children.Add(view);

            window.ShowDialog();
        }
    }
}
